//
//  RegistView.m
//  YiXiProject
//
//  Created by iMac on 2019/7/15.
//  Copyright © 2019 iMac. All rights reserved.
//

#import "RegistView.h"

@implementation RegistView

-(UIImageView *)imgmageView
{
    if(!_imgmageView)
    {
        _imgmageView = [[UIImageView alloc] init];
        _imgmageView.backgroundColor = WhiteColor;
        _imgmageView.layer.cornerRadius = 10;
        _imgmageView.layer.masksToBounds = YES;
        _imgmageView.userInteractionEnabled = YES;
        
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(imgClick:)];
        [_imgmageView addGestureRecognizer:tap];
    }
    return _imgmageView;
}

-(UILabel *)textLabel
{
    if(!_textLabel)
    {
        _textLabel = [[UILabel alloc] init];
        _textLabel.backgroundColor = ClearColor;
        _textLabel.text = @"Select Photo";
        _textLabel.textAlignment = NSTextAlignmentCenter;
        _textLabel.font = FontCustom(25, 1);
    }
    return _textLabel;
}

-(UITextField *)nameText
{
    if(!_nameText)
    {
        _nameText = [[UITextField alloc] init];
        _nameText.backgroundColor = WhiteColor;
        _nameText.font = Font(13);
        
        // 富文本
        NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"Enter full name"] attributes:
                                          @{NSForegroundColorAttributeName:BlackColor,
                                            NSFontAttributeName:_nameText.font
                                            }];
        _nameText.attributedPlaceholder = attrString;
        _nameText.layer.cornerRadius = 20;
        _nameText.layer.masksToBounds = YES;
        _nameText.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 40)];
        
        // 总是显示
        _nameText.leftViewMode = UITextFieldViewModeAlways;
        _nameText.clearButtonMode = UITextFieldViewModeWhileEditing;
        _nameText.tag = 100;
        _nameText.delegate = self;
    }
    return _nameText;
}

-(UITextField *)emailText
{
    if(!_emailText)
    {
        _emailText = [[UITextField alloc] init];
        _emailText.backgroundColor = WhiteColor;
        _emailText.font = Font(13);
        NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"Enter email"] attributes:
                                          @{NSForegroundColorAttributeName:BlackColor,
                                            NSFontAttributeName:_nameText.font
                                            }];
        _emailText.attributedPlaceholder = attrString;
        _emailText.layer.cornerRadius = 20;
        _emailText.layer.masksToBounds = YES;
        _emailText.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 40)];
        _emailText.leftViewMode = UITextFieldViewModeAlways;
        _emailText.clearButtonMode = UITextFieldViewModeWhileEditing;
        
        _emailText.tag = 101;
        _emailText.delegate = self;
    }
    return _emailText;
}

-(UITextField *)passText
{
    if(!_passText)
    {
        _passText = [[UITextField alloc] init];
        _passText.backgroundColor = WhiteColor;
        _passText.font = Font(13);
        _passText.secureTextEntry = YES;
        NSAttributedString *attrString = [[NSAttributedString alloc] initWithString:[NSString stringWithFormat:@"Enter password"] attributes:
                                          @{NSForegroundColorAttributeName:GrayColor,
                                            NSFontAttributeName:_nameText.font
                                            }];
        _passText.attributedPlaceholder = attrString;
        _passText.layer.cornerRadius = 20;
        _passText.layer.masksToBounds = YES;
        _passText.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, 40)];
        _passText.leftViewMode = UITextFieldViewModeAlways;
        _passText.clearButtonMode = UITextFieldViewModeWhileEditing;
        
        _passText.tag = 102;
        _passText.delegate = self;
        
    }
    return _passText;
}

-(UIButton *)registButton
{
    if(!_registButton)
    {
        _registButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [_registButton setTitle:@"Register" forState:UIControlStateNormal];
        [_registButton setTitleColor:WhiteColor forState:UIControlStateNormal];
        [_registButton setBackgroundColor:Color(0.8,0,0.3,1.0)];
        _registButton.titleLabel.font = Font(15);
        _registButton.layer.cornerRadius = 20;
        _registButton.layer.masksToBounds = YES;
        _registButton.tag = 101;
        [_registButton addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _registButton;
}

-(UIButton *)goLoginButton
{
    if(!_goLoginButton)
    {
        _goLoginButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        [_goLoginButton setTitle:@"Go login" forState:UIControlStateNormal];
        [_goLoginButton setTitleColor:WhiteColor forState:UIControlStateNormal];
        _goLoginButton.titleLabel.font = Font(15);
        _goLoginButton.tag = 102;
        [_goLoginButton addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _goLoginButton;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if(self = [super initWithFrame:frame])
    {
        [self addSubview:self.imgmageView];
        [self.imgmageView addSubview:self.textLabel];
        [self addSubview:self.nameText];
        [self addSubview:self.emailText];
        [self addSubview:self.passText];
        [self addSubview:self.registButton];
        [self addSubview:self.goLoginButton];
    }
    return self;
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    float heightV;
    if(SCREEN_HEIGHT > 736)
    {
        heightV = 88 + 50;
    }
    else
    {
        heightV = 64 + 50;
    }
    
    
    _imgmageView.sd_layout.leftSpaceToView(self, 50).topSpaceToView(self, heightV).rightSpaceToView(self, 50).heightIs(SCREEN_WIDTH-100);
    _textLabel.sd_layout.leftSpaceToView(_imgmageView, 0).topSpaceToView(_imgmageView, 0).rightSpaceToView(_imgmageView, 0).bottomSpaceToView(_imgmageView, 0);
    _nameText.sd_layout.leftSpaceToView(self, 50).topSpaceToView(_imgmageView, 15).rightSpaceToView(self, 50).heightIs(50);
    _emailText.sd_layout.leftSpaceToView(self, 50).topSpaceToView(_nameText, 10).rightSpaceToView(self, 50).heightIs(50);
    _passText.sd_layout.leftSpaceToView(self, 50).topSpaceToView(_emailText, 10).rightSpaceToView(self, 50).heightIs(50);
    _registButton.sd_layout.leftSpaceToView(self, 50).topSpaceToView(_passText, 20).rightSpaceToView(self, 50).heightIs(50);
    _goLoginButton.sd_layout.leftSpaceToView(self, 50).bottomSpaceToView(self, 50).rightSpaceToView(self, 50).heightIs(30);
}

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    textField.textColor = BlackColor;
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    switch (textField.tag) {
        case 100:
        {
            if(![Regular isVerificationUserName:textField.text])
            {
                textField.textColor = RedColor;
            }
        }
            break;
              
        case 101:
        {
            if (![Regular isVerificationEmail:textField.text])
            {
                textField.textColor = RedColor;
            }
        }
            break;
            
        case 102:
        {
            if (self.passText.text.length < 6 || self.passText.text.length > 20)
            {
                textField.textColor = RedColor;
            }
        }
            break;
        default:
            break;
    }
    
}



#pragma mark -- 选择头像执行这里
-(void)imgClick:(UIGestureRecognizer *)tapp
{
    if([_delegate respondsToSelector:@selector(selectHeadImg)])
    {
        [_delegate selectHeadImg];
    }
}

-(void)clickBtn:(UIButton *)btn
{
    
    if ([Regular isVerificationUserName:self.nameText.text] && [Regular isVerificationEmail:self.emailText.text] && self.passText.text.length >= 6 && self.passText.text.length <=20 )
    {
        _isSuccess = 1;
    }
    else
    {
        _isSuccess = 0;
    }

    if(btn.tag == 101)
    {
        if(!_isSuccess)
        {
            [SVProgressHUD showErrorWithStatus:@"注册信息填写不正确"];
            [SVProgressHUD dismissWithDelay:SVPROGRESSHUDTIME];
        }
        else
        {
            if([_delegate respondsToSelector:@selector(clickregistPageButton:)])
            {
                [_delegate clickregistPageButton:btn.tag];
            }
        }
    }
    else
    {
        if([_delegate respondsToSelector:@selector(clickregistPageButton:)])
        {
            [_delegate clickregistPageButton:btn.tag];
        }
    }

    
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
